
package com.ninthgroup.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * @create by: 1452863884@qq.com rcg
 * @description: 登录过滤器
 * @create time: 2022/10/13 20:48
 * @param
 * @return
 */

@WebFilter(urlPatterns = "/*")
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
         HttpServletRequest request= (HttpServletRequest) servletRequest;
         HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        String path = request.getRequestURI();
        if(path.equals("/login.jsp")||path.equals("/login/user")||path.endsWith(".css")
                ||path.endsWith(".js")||path.endsWith(".jpg")||path.endsWith(".png")
                ||path.endsWith("/adminLogin.jsp")||path.endsWith("/doctorLogin.jsp")
                ||path.equals("/login/doctor")||path.equals("/login/admin")
                ||path.equals("/homepage.html")||path.equals("/login/quit")
                ||path.equals("/image/*")||path.equals("/imags/*")
                ||path.equals("/image/6.webp")||path.equals("/image/7.webp")
                ||path.equals("/image/8.webp")||path.equals("/image/1.webp")
                ||path.equals("/image/2.webp")||path.equals("/email/send")
                ||path.equals("/sex/all")||path.equals("/code/outcode")
                ||path.equals("/code/getValidateCode")||path.equals("/userRegister.html")

        ){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }

        if(session.getAttribute("user")!=null){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        if(session.getAttribute("doctor")!=null){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        if(session.getAttribute("admin")!=null){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        response.sendRedirect("/homepage.html");

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
}

